package com.blog.wu.myblog.rocketmq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.util.StopWatch;


/**
 * @Author wuyanshen
 * @Date 2018-12-26 2:05 PM
 * @Discription rocketMQ生产者
 */
public class RocketMQProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer();
        producer.setProducerGroup("rmq-group");
        producer.setNamesrvAddr("192.168.0.121:9876");
        producer.setInstanceName("rmq-instance");
        producer.setCreateTopicKey("AUTO_CREATE_TOPIC_KEY");
        producer.start();

//        StopWatch stop = new StopWatch();
//        stop.start();


        //创建一个消息实例，包含 topic、tag 和 消息体
        //如下：topic 为 "TopicTest"，tag 为 "push"
        for(int i =0;i<10;i++){
            Thread.sleep(1000);//每秒发送一次MQ
            Message message = new Message("myblog-rmq","push",("我是生产者：生产消息 "+i+"--"+System.currentTimeMillis()).getBytes());
            producer.send(message);
        }
        producer.shutdown();
//        stop.stop();
    }
}
